# -*- coding: utf-8 -*-
# Project : flacon
# File : encoder.py
# (c) Alexander Sokolov sokoloff.a@gmail.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.


#import errno
import exceptions
import re
import sys
import time


from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *

import encoder
from settings import *
import flaconglobal

class EncoderConfigPage(QtGui.QWidget):
   #****************************************************************
   #
   #****************************************************************
    def __init__(self, parent):
        QtGui.QWidget.__init__(self, parent)


   #****************************************************************
   #
   #****************************************************************
    def write(self):
        raise TypeError('Abstract method "EncoderConfigPage.write" called')


   #****************************************************************
   #
   #****************************************************************
    @staticmethod
    def defaults():
        raise TypeError('Abstract method "EncoderConfigPage.defaults" called')



#*******************************************************************
#   Helper funcs
#*******************************************************************

   #****************************************************************
   #
   #****************************************************************
    def fillReplayGainComboBox(self, comboBox):
        comboBox.clear()
        comboBox.addItem(QtGui.qApp.translate('EncoderConfigPage', 'Disabled'),  QVariant(flaconglobal.ReplayGainTypes.Disable))
        comboBox.addItem(QtGui.qApp.translate('EncoderConfigPage', 'Per Track'), QVariant(flaconglobal.ReplayGainTypes.Track))
        comboBox.addItem(QtGui.qApp.translate('EncoderConfigPage', 'Per Album'), QVariant(flaconglobal.ReplayGainTypes.Album))
        comboBox.setToolTip(QtGui.qApp.translate('EncoderConfigPage',
                "ReplayGain is a standard to normalize the perceived loudness of computer audio formats. \n\n" +
                "The analysis can be performed on individual tracks, so that all tracks will be of equal volume on playback. \n"+
                "Using the album-gain analysis will preserve the volume differences within an album."
            ))


   #****************************************************************
   #
   #****************************************************************
    def fillBitrateComboBox(self, comboBox, bitrates):
        for bitrate in bitrates:
            if (bitrate):
                comboBox.addItem(
                        QtGui.qApp.translate('EncoderConfigPage', '%1 kbps').arg(bitrate),
                        QVariant(bitrate)
                    )
            else:
                comboBox.addItem(
                        QtGui.qApp.translate('EncoderConfigPage', 'Default'),
                        QVariant('')
                    )


   #****************************************************************
   #
   #****************************************************************
    @classmethod
    def loadComboBox(cls, key, comboBox):
        n = comboBox.findData(settings.value(key))

        if n < 0:
            print settings.defaultValue(key).toString()
            n = comboBox.findData(settings.defaultValue(key))
            print n
        comboBox.setCurrentIndex(n)



   #****************************************************************
   #
   #****************************************************************
    @classmethod
    def writeComboBox(cls, key, comboBox):
        data = comboBox.itemData(comboBox.currentIndex())
        settings.setValue(key, data)


   #****************************************************************
   #
   #****************************************************************
    @classmethod
    def loadSpinBox(cls, key, spinBox):
        if isinstance(spinBox, QDoubleSpinBox):
            (value, ok) = settings.value(key).toDouble()
            if ok:  spinBox.setValue(value)

        if isinstance(spinBox, QSpinBox):
            (value, ok) = settings.value(key).toInt()
            if ok:  spinBox.setValue(value)


   #****************************************************************
   #
   #****************************************************************
    @classmethod
    def writeSpinBox(cls, key, spinBox):
         settings.setValue(key, spinBox.value())


   #****************************************************************
   #
   #****************************************************************
    def losslessCompressionToolTip(self, min, max):
        return QtGui.qApp.translate('EncoderConfigPage',
                "Sets compression level, between %1 (fastest) and %2 (highest compression).\n" +
                "This only affects the file size. All settings are lossless."
                ).arg(min).arg(max)


   #****************************************************************
   #
   #****************************************************************
    def lossyCompressionToolTip(self, min, max):
        return QtGui.qApp.translate('EncoderConfigPage',
                "Sets encoding quality, between %1 (lowest) and %2 (highest)."
                ).arg(min).arg(max)


   #****************************************************************
   #
   #****************************************************************
    def setLossyToolTip(self, widget):
        widget.setToolTip(self.lossyCompressionToolTip(widget.minimum(), widget.maximum()))


   #****************************************************************
   #
   #****************************************************************
    def setLosslessToolTip(self, widget):
        widget.setToolTip(self.losslessCompressionToolTip(widget.minimum(), widget.maximum()))


   #****************************************************************
   #
   #****************************************************************
    @classmethod
    def loadSlider(cls, key, slider):
        (value, ok) = settings.value(key).toInt()
        if ok:
            slider.setValue(value)


   #****************************************************************
   #
   #****************************************************************
    @classmethod
    def writeSlider(cls, key, slider):
        settings.setValue(key, slider.value())


   #****************************************************************
   #
   #****************************************************************
    @classmethod
    def loadLineEdit(cls, key, edit):
        edit.setText(settings.value(key).toString())


   #****************************************************************
   #
   #****************************************************************
    @classmethod
    def writeLineEdit(cls, key, edit):
        settings.setValue(key, QVariant(edit.text()))


   #****************************************************************
   #
   #****************************************************************
    @classmethod
    def loadCheckBox(cls, key, checkBox):
        value = settings.value(key).toBool()
        checkBox.setChecked(value)


   #****************************************************************
   #
   #****************************************************************
    @classmethod
    def writeCheckBox(cls, key, checkBox):
        settings.setValue(key, checkBox.isChecked())
